// pages/yaoList/yaoList.js
Page({

      /**
       * 页面的初始数据
       */
      data: {
            list:[]
      },
      initList(list){
            list.forEach(item => {
                  item.invitee=JSON.parse(item.invitee);
                  if(new Date().getTime()>item.loseDate){
                        item.status=0
                  }else{
                        item.status=1
                  }
                  // 初始化时间
                  item.date=this.formatTime(item.loseDate)
            });
            this.setData({
                  list:list
            })
      },
      // 格式化日期
      formatTime(time){
            let date=new Date(parseInt(time));
            let y=date.getFullYear();
            let m=(date.getMonth()+1)<10?('0'+(date.getMonth()+1)):(date.getMonth()+1);
            let d=date.getDate()<10?'0'+date.getDate():date.getDate();
            return y+'-'+m+'-'+d
      },
      // 立即失效
      handleLose(e){
            wx.showModal({
                  title: '提示',
                  content: '该邀请卡将立即失效',
                  success:res=> {
                        if (res.confirm) {
                              wx.showLoading({
                                    title: '加载中',
                              })
                              let inviteInfo={
                                    id:e.currentTarget.dataset.id,
                                    userId:wx.getStorageSync('userId')
                              }
                  
                              wx.request({
                                    url: getApp().globalData.host+'/client/invite/handleLose',
                                    method:'POST',
                                    data:inviteInfo,
                                    header: {
                                          'content-type': 'application/x-www-form-urlencoded'
                                    },
                                    success:res=>{
                                          this.initList(res.data.data.inviteList);
                                          wx.showToast({
                                            title: '已失效'
                                          })
                                    },
                                    complete:()=>{
                                          wx.hideLoading();
                                    }
                              })
                        } else if (res.cancel) {
                              
                        }
                  }
            })

      },
      // 删除邀请卡
      handleDel(e){
            wx.showModal({
                  title: '提示',
                  content: '删除后无法恢复，确认继续吗？',
                  success :res=> {
                        if (res.confirm) {
                              wx.showLoading({
                                    title: '加载中',
                              })
                              let inviteInfo={
                                    id:e.currentTarget.dataset.id,
                                    userId:wx.getStorageSync('userId')
                              }
                              wx.request({
                                    url: getApp().globalData.host+'/client/invite/delInvite',
                                    method:'DELETE',
                                    data:inviteInfo,
                                    header: {
                                          'content-type': 'application/x-www-form-urlencoded'
                                    },
                                    success:res=>{
                                          this.initList(res.data.data.inviteList);
                                          wx.showToast({
                                                title: '已经删除'
                                          })
                                    },
                                    complete:()=>{
                                          wx.hideLoading();
                                    }
                              })
                        } else if (res.cancel) {
                              
                        }
                  }
            })
      },

      /**
       * 生命周期函数--监听页面加载
       */
      onLoad: function (options) {

      },

      /**
       * 生命周期函数--监听页面初次渲染完成
       */
      onReady: function () {

      },

      /**
       * 生命周期函数--监听页面显示
       */
      onShow: function () {
            this.init();
      },
      init(){
            wx.showLoading({
                  title: '加载列表',
            })
            let id=wx.getStorageSync('userId');
            wx.request({
                  url: getApp().globalData.host+'/client/invite/list?id='+id,
                  success:res=>{
                        this.initList(res.data.data.inviteList)
                  },
                  complete:()=>{
                        wx.hideLoading();
                  }
            })
      }
})